export const addThousandsSeparator = (number) => {
    return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

/**
 * 时间戳转时间  YYYY-MM-DD
 * @param time
 */
export const formatYMDhms = (time)=> {
  if (typeof (time) == 'undefined') {
    return ''
  } else {
    let date = new Date(parseInt(time))
    let y = date.getFullYear()
    let MM = date.getMonth() + 1
    MM = MM < 10 ? ('0' + MM) : MM
    let d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    // let h = date.getHours()
    // h = h < 10 ? ('0' + h) : h
    // let m = date.getMinutes()
    // m = m < 10 ? ('0' + m) : m
    // let s = date.getSeconds()
    // s = s < 10 ? ('0' + s) : s
    
    return y + '年' + MM + '月' + d + '日'
  }
}
/**
 * 获取周几  星期X
 * @param time
 */
export const getWeekDate = ()=> {
  var now = new Date();
  var day = now.getDay();
  var weeks = new Array(
    "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六"
  );
  var week = weeks[day];
  return week;
}
/**
 * 获取当前时间  xx:xx:xx
 * @param time
 */
export const getNowTime = () => {
  var now = new Date();
  var on1 = ":";
  var onS = now.getHours(); //时
  var onF = now.getMinutes(); //分
  var onN = now.getSeconds(); //秒
  if (onS >= 0 && onS <= 9) {
    //时
    onS = "0" + onS;
  }
  if (onF >= 0 && onF <= 9) {
    //分
    onF = "0" + onF;
  }
  if (onN >= 0 && onN <= 9) {
    //秒
    onN = "0" + onN;
  }
  return {
    time: onS + on1 + onF + on1 + onN
  }
}

export const addThousands = (number) =>{
  return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

export const getDateRangeString = () => {
  const today = new Date();
  const options = { year: 'numeric', month: 'long', day: 'numeric' };
  
  // 获取昨天的日期
  const yesterday = new Date(today);
  yesterday.setDate(yesterday.getDate() - 1);
  const yesterdayStr = yesterday.toLocaleDateString('zh-CN', options);

  // 获取7天前的日期
  const sevenDaysAgo = new Date(today);
  sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
  const sevenDaysAgoStr = sevenDaysAgo.toLocaleDateString('zh-CN', options);

  // 获取30天前的日期
  const thirtyDaysAgo = new Date(today);
  thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
  const thirtyDaysAgoStr = thirtyDaysAgo.toLocaleDateString('zh-CN', options);

  // 格式化输出
  const yesterdayOutput = yesterdayStr;
  const lastSevenDaysOutput = `${sevenDaysAgoStr}～${today.toLocaleDateString('zh-CN', options)}`;
  const lastThirtyDaysOutput = `${thirtyDaysAgoStr}～${today.toLocaleDateString('zh-CN', options)}`;

  return {
    'yesterday': yesterdayOutput,
    'lastSevenDays': lastSevenDaysOutput,
    'lastThirtyDays': lastThirtyDaysOutput,
    'today': today.toLocaleDateString('zh-CN', options)
  };
}

export const getLast7Days = () => {
  const result = [];
  const today = new Date();
  for (let i = 6; i >= 0; i--) {
    const date = new Date(today);
    date.setDate(today.getDate() - i); // 设置日期为今天减去 i 天
    result.push(date.toISOString().split('T')[0]); // 转换为 YYYY-MM-DD 格式
  }
  return result;
}
export const getLast15Days = () => {
  const result = [];
  const today = new Date();
  for (let i = 14; i >= 0; i--) {
    const date = new Date(today);
    date.setDate(today.getDate() - i); // 设置日期为今天减去 i 天
    result.push(date.toISOString().split('T')[0]); // 转换为 YYYY-MM-DD 格式
  }
  return result;
}
export const getLast30Days = () => {
// 获取今天的日期
const today = new Date();
// 函数：格式化日期为 YYYY-MM-DD
function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');  // 月份从0开始，所以加1
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}
  // 获取最近30天的日期
  let last30Days = [];
  for (let i = 0; i < 30; i++) {
      const date = new Date(today);
      date.setDate(today.getDate() - i);  // 将日期向后推
      last30Days.push(formatDate(date));  // 格式化并加入数组
  }
  last30Days = last30Days.reverse()
  return last30Days;
}
export const getLast60Days = () => {
  // 获取今天的日期
  const today = new Date();
  // 函数：格式化日期为 YYYY-MM-DD
  function formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');  // 月份从0开始，所以加1
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
  }
    // 获取最近60天的日期
    let last60Days = [];
    for (let i = 0; i < 60; i++) {
        const date = new Date(today);
        date.setDate(today.getDate() - i);  // 将日期向后推
        last60Days.push(formatDate(date));  // 格式化并加入数组
    }
    last60Days = last60Days.reverse()
    return last60Days;
  }
export const getYesterday = () => {
  const today = new Date();
  today.setDate(today.getDate() - 1);
  // 获取昨天的年、月、日
  const year = today.getFullYear();
  const month = (today.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始，所以加1
  const day = today.getDate().toString().padStart(2, '0');
  const yesterday = `${year}${month}${day}`;
  return yesterday; // 输出昨天的日期，格式为'yymmdd'
}
export const getToday = () => {
  const today = new Date();
  today.setDate(today.getDate());
  // 获取昨天的年、月、日
  const year = today.getFullYear();
  const month = (today.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始，所以加1
  const day = today.getDate().toString().padStart(2, '0');
  const getToday = `${year}-${month}-${day}`;
  return getToday;
}